package com.carrier.springboot.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.carrier.springboot.common.vo.RestResponse;
import com.carrier.springboot.common.vo.RestResult;
import com.carrier.springboot.entity.User;
import com.carrier.springboot.mapper.UserMapper;
import com.carrier.springboot.service.IUserService;
import com.github.pagehelper.PageInfo;
 

/**
 * 
 * @ClassName: UserController
 * @Description: TODO(User CRUD)
 * @author Carrier
 * @date 2018年9月12日
 *
 */
@RestController
public class UserController {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private IUserService userService;

	@Autowired
	private UserMapper userMapper;

	@ResponseBody
	@RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
	public User getUser(@PathVariable String id) {
		return userService.selectUserById(Integer.valueOf(id));
	}

	@ResponseBody
	@RequestMapping(value = "/addUser")
	public User addUser() {
		logger.info("addUser start info");
		logger.debug("addUser start debug");
		logger.error("addUser start error");
		User user = new User();
		user.setName("Carrier" + new Date());
		user.setSex("male");
		user.setCompany("DGH_GLOBAL.COM");
		user.setDept("IT");
		user.seteMail("524946398@qq.com");
		userMapper.insert(user);
		return user;
	}

	@ResponseBody
	@RequestMapping(value = "/delUser/{id}")
	public String delUser(@PathVariable String id) {
		userMapper.deleteByPrimaryKey(Integer.valueOf(id));
		return "success";
	}

	@RequestMapping(value = "/updateUser/{id}")
	public User updateUser(@PathVariable String id) {
		User user = userService.selectUserById(Integer.valueOf(id));
		user.setName("Carrier" + new Date());
		userMapper.updateByPrimaryKey(user);
		return user;
	}
	/**
	    * @Title: findAll
	    * @Description: TODO(场景I)
	    * @param @param pageNum
	    * @param @param pageSize
	    * @param @return    参数
	    * @return List<User>    返回类型
	    * @throws
	 */
	@RequestMapping(value="/findAll")
	public List<User> findAll(@RequestParam int pageNum, @RequestParam int pageSize){
		return userService.findAll(pageNum, pageSize);
	}
	/**
	    * @Title: selectAll
	    * @Description: TODO(简单封闭)
	    * @param @param pageNum
	    * @param @param pageSize
	    * @param @return    参数
	    * @return RestResult<PageInfo<User>>    返回类型
	    * @throws
	 */
	@RequestMapping(value="/selectAll")
	public RestResult<PageInfo<User>>  selectAll(@RequestParam int pageNum, @RequestParam int pageSize){
		return RestResponse.responseOK(new PageInfo<>(userService.findAll(pageNum, pageSize)));
	}
	
	/**
	 * 
	    * @Title: login
	    * @Description: TODO(测试shiro 登录)
	    * @param @param userName
	    * @param @param password
	    * @param @return    参数
	    * @return RestResult    返回类型
	    * @throws
	 */
	@RequestMapping(value="/login")
	public <T> RestResult login(@RequestParam String userName, @RequestParam String password){
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
		try {
            subject.login(token);
        } catch (AuthenticationException e) {
            token.clear();
            return RestResponse.responseError("登录失败，用户名或密码错误！");
        }
        return RestResponse.responseOK("登录成功");
	}

}
